/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.geometry;

import Engine2D.math.Matrix2f;
import Engine2D.math.Matrix3f;
import Engine2D.math.Vector2f;

/**
 *
 * @author Garys
 */
public class  Transform {
    
    private float matrixPosition[];  
    Matrix3f matrix3f ;
    
    /**
     * 
     */
    public Transform() {   
        matrixPosition = new float[]{1, 0, 0, 0, 1, 0, 0, 0, 1};   
         matrix3f = new Matrix3f(matrixPosition);
    }  
    
    /**
     * 
     * @param matrixPosition
     */
    public Transform(float matrixPosition[]) {   
        if(matrixPosition.length != 6) {   
            throw new RuntimeException("The parameter must be a float array of length 6.");   
        }   
        this.matrixPosition = new float[]{matrixPosition[0], matrixPosition[1], matrixPosition[2],    
                matrixPosition[3], matrixPosition[4], matrixPosition[5],    
                0f, 0f, 1f};   
        
        matrix3f = new Matrix3f(this.matrixPosition);
    }  
    
    
    
    /**
     * 
     * @param source
     * @param sourceOffset
     * @param destination
     * @param destOffset
     * @param numberOfPoints
     */
    public void transform(float source[], int sourceOffset, float destination[], int destOffset, int numberOfPoints) {
        float result[] = source;
        
        for(int i=0;i<numberOfPoints * 2;i+=2) {   
            for(int j=0;j<6;j+=3) {   
                result[i + (j / 3)] = source[i + sourceOffset] * matrixPosition[j] + source[i + sourceOffset + 1] * matrixPosition[j + 1] + 1 * matrixPosition[j + 2];   
            }   
        }  
        
        if (source == destination) {
	        //for safety of the destination, the results are copied after the entire operation.   
	        for(int i=0;i<numberOfPoints * 2;i+=2) {   
	            destination[i + destOffset] = result[i];   
	            destination[i + destOffset + 1] = result[i + 1];   
	        }   
        }
        
    }
    
    
    /**
     * 
     * @param source
     * @param sourceOffset
     * @param destination
     * @param destOffset
     * @param numberOfPoints
     */
    public void transform2(float source[], int sourceOffset, float destination[], int destOffset, int numberOfPoints) {
        float result[] = source;
        
        
        System.out.println("matrix set....");
        matrix3f.setZero();
       //System.out.println(matrix3f.toString());
        matrix3f.rotateZ(Math.toRadians(1));
        System.out.println(matrix3f.toString());
        for(int i=0;i<numberOfPoints * 2;i+=2) {   
            
                System.out.println("..");
                Vector2f vector2f = new Vector2f(result[i], result[i+1]);
                System.out.println(vector2f.getX() + " " + vector2f.getY());
                
                vector2f  = matrix3f.transform3x2(vector2f);
                result[i] = vector2f.getX();
                result[i+1] = vector2f.getY();
                System.out.println(vector2f.getX() + " " + vector2f.getY());
               
        }  
        
        
        
        
        
        
        
        
        
        if (source == destination) {
	        //for safety of the destination, the results are copied after the entire operation.   
	        for(int i=0;i<numberOfPoints * 2;i+=2) {   
	            destination[i + destOffset] = result[i];   
	            destination[i + destOffset + 1] = result[i + 1];   
	        }   
        }
        
        
    }
    
    /**
     * 
     * @param source
     * @param sourceOffset
     * @param destination
     * @param destOffset
     * @param numberOfPoints
     */
    public void transform3(float source[], int sourceOffset, float destination[], int destOffset, int numberOfPoints) {
        float result[] = source;
        System.out.println("matrix set....");
        matrix3f.setZero();
       System.out.println(matrix3f.toString());
        matrix3f.rotateX(Math.toRadians(-12));
        
        
        System.out.println(matrix3f.toString());
        
        for(int i=0;i<numberOfPoints * 2;i+=2) {   
            
                System.out.println("..");
                Vector2f vector2f = new Vector2f(result[i], result[i+1]);
                System.out.println(vector2f.getX() + " " + vector2f.getY());
                
                vector2f  = matrix3f.transform3x2(vector2f);
                result[i] = vector2f.getX();
                result[i+1] = vector2f.getY();
                System.out.println(vector2f.getX() + " " + vector2f.getY());
               
        }  
        
        
        
        
        
        
        
        
        
        if (source == destination) {
	        //for safety of the destination, the results are copied after the entire operation.   
	        for(int i=0;i<numberOfPoints * 2;i+=2) {   
	            destination[i + destOffset] = result[i];   
	            destination[i + destOffset + 1] = result[i + 1];   
	        }   
        }
        
        
    }
    
    
}
